package com.koron.common.web.mapper;

import com.koron.common.bean.StaffBean;
import com.koron.common.web.bean.DepartmentBean;
import com.koron.common.web.bean.DepartmentTreeBean;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.koron.ebs.mybatis.EnvSource;

import java.util.List;

/**
 * 后面删除
 */
@EnvSource("_default")
public interface DepartmentMapper {
    @Select("select tbltree.seq,tbltree.parentmask,tbldepartment.* from tbltree \n" +
            "inner join tbldepartment on tbltree.foreignkey=tbldepartment.id \n" +
            "inner join (\n" +
            "	select tbltree.* from tbltree \n" +
            "where type = 65536 and seq =#{id}) a on (tbltree.seq & ~((1 << (62 - a.parentMask-a.mask))-1)) = a.seq")
    public List<DepartmentTreeBean> getDescendantByParentId(@Param("id") Long id);

    @Select("select tbltree.seq,tbltree.parentmask,tbldepartment.* from tbltree \n" +
            "inner join tbldepartment on tbltree.foreignkey=tbldepartment.id \n" +
            "where (#{seq} & ~((1 << (62 - parentmask-mask))-1)) = seq\n" +
            "order by seq\n")
    public List<DepartmentTreeBean> getPathById(@Param("seq") Long seq);

    @Select("select a.*,b.name as departmentname,b.code as departmentCode from tblstaff a left join tbldepartment b on a.departmentCode = b.code where b.code = #{department}")
    public List<StaffBean> getStaffOfDep(@Param("department") String code);

    @Select("select a.*,b.name as departmentname,b.code as departmentCode  from tblstaff a left join tbldepartment b on a.departmentCode = b.code")
    public List<StaffBean> getStaffInfo();

    @Select("SELECT a.* FROM tbldepartment a WHERE state = 0 ORDER BY a.sn DESC")
    public List<DepartmentBean> getDepartmentInfo();

    @Select("select tbldepartment.* from tbldepartment where state = 0 and code = #{parentCode}")
    public DepartmentBean getDepartmentByParentCode(String parentCode);

    @Delete("DELETE FROM tbldepartment")
    int deleteAll();

    @Select("select tbltree.seq,tbltree.parentmask,tbldepartment.* from tbltree \n" +
            "inner join tbldepartment on tbltree.foreignkey=tbldepartment.id \n" +
            "where flag = #{flag} order by seq ")
    public List<DepartmentTreeBean> getDepartmentInfoByFlag(@Param("flag") int flag);

    @Select("select * from tbldepartment where code = #{code}")
    public DepartmentBean getDepartmentInfoByCode(@Param("code") String waterCode);

    public int batchInsertDepartment(List<DepartmentBean> list);

    public int updateDepartment(DepartmentBean bean);

}